package com.junhong.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Repository;

import com.junhong.dao.StudentDao;
import com.junhong.entity.Student;

@Repository
public class StudentDaoImpl implements StudentDao {

	@Resource
	private SessionFactory sessionFactory;

	@Override
	public void add(Student student) {
		sessionFactory.getCurrentSession().save(student);
	}

	@Override
	public void update(Student student) {
		sessionFactory.getCurrentSession().update(student);
	}

	@Override
	public void delete(Student student) {
		sessionFactory.getCurrentSession().delete(student);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Student> find() {
		return sessionFactory.getCurrentSession().createCriteria(Student.class).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Student> find(Integer pos, Integer rows) {
		return sessionFactory.getCurrentSession().createCriteria(Student.class).setFirstResult(pos).setMaxResults(rows).list();
	}

	@Override
	public Student getById(Serializable id) {
		return (Student) sessionFactory.getCurrentSession().get(Student.class, id);
	}

	@Override
	public long count() {
		return (long) sessionFactory.getCurrentSession().createCriteria(Student.class).setProjection(Projections.rowCount()).uniqueResult();
	}

}
